package com.itheima.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.itheima.pojo.Blog;
import com.itheima.service.BlogService;
import com.itheima.utils.MessageConstant;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.BlogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 博客相关接口
 */
@RestController
@RequestMapping("/blog")
@Api(tags = "博客相关接口")
public class BlogController {
    @Autowired
    BlogService blogService;

    /**
     * 新增博客
     *
     * @param blogVO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增博客")
    public String add(@RequestBody BlogVO blogVO) {
        try {
            blogService.add(blogVO);
            return ResultUtil.result("success", MessageConstant.BLOG_ADD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result("error", MessageConstant.BLOG_ADD_FAIL);
        }
    }


    /**
     * 编辑博客
     *
     * @param blogVO
     * @return
     */

    @PostMapping("/edit")
    @ApiOperation("编辑博客")
    public String edit(@RequestBody BlogVO blogVO) {
        if (blogVO.getUid() == null || "".equals(blogVO.getUid())) {
            return ResultUtil.result("error", "请选择要编辑的博客");

        }
        try {
            blogService.edit(blogVO);
            return ResultUtil.result("error", MessageConstant.BLOG_EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result("error", MessageConstant.BLOG_EDIT_FAIL);
        }
    }

    //删除博客
    @PostMapping("/delete")
    @ApiOperation("删除博客")
    public String delete(@RequestBody BlogVO blogVO) {
        if (blogVO.getUid() == null || "".equals(blogVO.getUid())) {
            return ResultUtil.result("error", "请选择要删除的博客");
        }
        try {
            blogService.delete(blogVO);
            return ResultUtil.result("success", MessageConstant.BLOG_DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result("error", MessageConstant.BLOG_DELETE_FAIL);
        }
    }

    //删除选中博客
    @PostMapping("/deleteBatch")
    @ApiOperation("删除选中博客")
    public String deleteBatch(@RequestBody BlogVO blogVO) {
        if (blogVO.getTagList() == null || blogVO.getTagList().size() <= 0) {
            return ResultUtil.result("error", "请选择要删除的博客");
        }
        try {
            blogService.deleteBatch(blogVO);
            return ResultUtil.result("success", MessageConstant.BLOG_DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result("error", MessageConstant.BLOG_DELETE_FAIL);
        }
    }

    /**
     * 查询所有及分页查询和条件查询
     *
     * @param blogVO
     * @return
     */
    @PostMapping("/getList")
    @ApiOperation("查询所有博客")
    public String getList(@RequestBody BlogVO blogVO) {
        try {
            List<Blog> blogList = blogService.getList(blogVO);
            Map<String,Object> map =new HashMap<String, Object>();
            map.put("records", ((Page<Blog>) blogList).getResult());
            map.put("total", ((Page<Blog>) blogList).getTotal());
            map.put("size", ((Page<Blog>) blogList).getPageSize());
            map.put("current", ((Page<Blog>) blogList).getPageNum());
            map.put("orders", null);
            map.put("optimizeCountSql", true);
            map.put("isSearchCount", true);

            return ResultUtil.result("success", map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result("error", MessageConstant.BLOG_FINDALL_FAIL);
        }

    }
}
